<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品ID" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购类型" prop="purchaseCategory">
        <el-input
          v-model="queryParams.purchaseCategory"
          placeholder="请输入采购类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机种" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入机种"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出厂编号" prop="manufactureNumber">
        <el-input
          v-model="queryParams.manufactureNumber"
          placeholder="请输入出厂编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出厂子编号" prop="manufactureSubNumber">
        <el-input
          v-model="queryParams.manufactureSubNumber"
          placeholder="请输入出厂子编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图号、型号" prop="drawingNumberModel">
        <el-input
          v-model="queryParams.drawingNumberModel"
          placeholder="请输入图号、型号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['scm:scmCustomerOrderItems:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scm:scmCustomerOrderItems:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scm:scmCustomerOrderItems:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['scm:scmCustomerOrderItems:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scmCustomerOrderItemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="itemId" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="产品ID" align="center" prop="productId" />
      <el-table-column label="采购类型" align="center" prop="purchaseCategory" />
      <el-table-column label="部品类型" align="center" prop="demandType" />
      <el-table-column label="机种" align="center" prop="model" />
      <el-table-column label="出厂编号" align="center" prop="manufactureNumber" />
      <el-table-column label="出厂子编号" align="center" prop="manufactureSubNumber" />
      <el-table-column label="图号、型号" align="center" prop="drawingNumberModel" />
      <el-table-column label="版本" align="center" prop="drawingVersion" />
      <el-table-column label="品名" align="center" prop="productName" />
      <el-table-column label="材质" align="center" prop="material" />
      <el-table-column label="尺寸" align="center" prop="size" />
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="颜色" align="center" prop="color" />
      <el-table-column label="单价类型" align="center" prop="unitPriceType" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="模具费" align="center" prop="moldCost" />
      <el-table-column label="工装费" align="center" prop="jigCost" />
      <el-table-column label="订单到货日" align="center" prop="requestedDeliveryDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.requestedDeliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到货日回复" align="center" prop="repliedDeliveryDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.repliedDeliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:scmCustomerOrderItems:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:scmCustomerOrderItems:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户订单明细对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="scmCustomerOrderItemsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品ID" />
        </el-form-item>
        <el-form-item label="采购类型" prop="purchaseCategory">
          <el-input v-model="form.purchaseCategory" placeholder="请输入采购类型" />
        </el-form-item>
        <el-form-item label="机种" prop="model">
          <el-input v-model="form.model" placeholder="请输入机种" />
        </el-form-item>
        <el-form-item label="出厂编号" prop="manufactureNumber">
          <el-input v-model="form.manufactureNumber" placeholder="请输入出厂编号" />
        </el-form-item>
        <el-form-item label="出厂子编号" prop="manufactureSubNumber">
          <el-input v-model="form.manufactureSubNumber" placeholder="请输入出厂子编号" />
        </el-form-item>
        <el-form-item label="图号、型号" prop="drawingNumberModel">
          <el-input v-model="form.drawingNumberModel" placeholder="请输入图号、型号" />
        </el-form-item>
        <el-form-item label="版本" prop="drawingVersion">
          <el-input v-model="form.drawingVersion" placeholder="请输入版本" />
        </el-form-item>
        <el-form-item label="品名" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入品名" />
        </el-form-item>
        <el-form-item label="材质" prop="material">
          <el-input v-model="form.material" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="尺寸" prop="size">
          <el-input v-model="form.size" placeholder="请输入尺寸" />
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="颜色" prop="color">
          <el-input v-model="form.color" placeholder="请输入颜色" />
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="模具费" prop="moldCost">
          <el-input v-model="form.moldCost" placeholder="请输入模具费" />
        </el-form-item>
        <el-form-item label="工装费" prop="jigCost">
          <el-input v-model="form.jigCost" placeholder="请输入工装费" />
        </el-form-item>
        <el-form-item label="订单到货日" prop="requestedDeliveryDate">
          <el-date-picker clearable
            v-model="form.requestedDeliveryDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择订单到货日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到货日回复" prop="repliedDeliveryDate">
          <el-date-picker clearable
            v-model="form.repliedDeliveryDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择到货日回复">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ScmCustomerOrderItems">
import { listScmCustomerOrderItems, getScmCustomerOrderItems, delScmCustomerOrderItems, addScmCustomerOrderItems, updateScmCustomerOrderItems } from "@/api/scm/scmCustomerOrderItems";

const { proxy } = getCurrentInstance();

const scmCustomerOrderItemsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    productId: null,
    purchaseCategory: null,
    demandType: null,
    model: null,
    manufactureNumber: null,
    manufactureSubNumber: null,
    drawingNumberModel: null,
  },
  rules: {
    orderId: [
      { required: true, message: "订单ID不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "产品ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询客户订单明细列表 */
function getList() {
  loading.value = true;
  listScmCustomerOrderItems(queryParams.value).then(response => {
    scmCustomerOrderItemsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    itemId: null,
    orderId: null,
    productId: null,
    purchaseCategory: null,
    demandType: null,
    model: null,
    manufactureNumber: null,
    manufactureSubNumber: null,
    drawingNumberModel: null,
    drawingVersion: null,
    productName: null,
    material: null,
    size: null,
    specification: null,
    color: null,
    unitPriceType: null,
    unitPrice: null,
    quantity: null,
    amount: null,
    moldCost: null,
    jigCost: null,
    requestedDeliveryDate: null,
    repliedDeliveryDate: null,
    remarks: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("scmCustomerOrderItemsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.itemId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加客户订单明细";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _itemId = row.itemId || ids.value
  getScmCustomerOrderItems(_itemId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改客户订单明细";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["scmCustomerOrderItemsRef"].validate(valid => {
    if (valid) {
      if (form.value.itemId != null) {
        updateScmCustomerOrderItems(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScmCustomerOrderItems(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _itemIds = row.itemId || ids.value;
  proxy.$modal.confirm('是否确认删除客户订单明细编号为"' + _itemIds + '"的数据项？').then(function() {
    return delScmCustomerOrderItems(_itemIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/scmCustomerOrderItems/export', {
    ...queryParams.value
  }, `scmCustomerOrderItems_${new Date().getTime()}.xlsx`)
}

getList();
</script>
